#include "statisticswidget.h"
#include "mybtn/backbtn.h"
#include "commondef.h"
#include "global/globalvar.h"
#include <QPainter>
#include <QDebug>
#include <QMouseEvent>

#define S_INTERVAL  40.0f

StatisticsWidget::StatisticsWidget(QWidget *parent) :
    QWidget(parent)
{
    m_backbtn = new BackBtn(this);
    m_backbtn->setGeometry(QRect(0,WINDOW_HEIGHT - 60,80,60));
    connect(m_backbtn,SIGNAL(myclick()),this,SLOT(statistomain()));
}

void StatisticsWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    QRectF rctext = QRectF(10.0f,0.0f,350.0f,S_INTERVAL);
    for(int i = 0; i < 4; ++i)
    {
        p.drawText(QRectF(10.0f,i * 3 * S_INTERVAL,350.0f,S_INTERVAL),
                   Qt::AlignVCenter | Qt::AlignLeft,
                   m_strfresh[i]);
        p.drawText(QRectF(10.0f,(i * 3 + 1) * S_INTERVAL ,350.0f,S_INTERVAL),
                   Qt::AlignVCenter | Qt::AlignLeft,
                   m_strt[i]);
        p.drawText(QRectF(10.0f,(i * 3 + 2) * S_INTERVAL ,350.0f,S_INTERVAL),
                   Qt::AlignVCenter | Qt::AlignLeft,
                   m_strwcsj[i]);
    }
}

void StatisticsWidget::setStatisData()
{
    QString strzjjl = tr("  Best record") + ":";
    QString strno = tr("no");
    QString strwcsj = tr("Completion time");
    m_strfresh[0] = globalvar::m_strfresh + strzjjl;
    m_strfresh[1] = globalvar::m_strnormal + strzjjl;
    m_strfresh[2] = globalvar::m_strmaster + strzjjl;
    m_strfresh[3] = globalvar::m_strgun + strzjjl;

    for(int i = 0; i < 4; ++i)
    {
        if(globalvar::levelDate(NEW_L + i) == "")
        {
            m_strt[i] = strno;
            m_strwcsj[i] = "";
        }
        else
        {
            m_strt[i] = globalvar::levelDate(NEW_L + i);
            m_strwcsj[i] = strwcsj + ": " + globalvar::levelTime(NEW_L + i).toString();
        }
    }
}

void StatisticsWidget::mousePressEvent(QMouseEvent *e)
{
    if(QRectF(0,WINDOW_HEIGHT - 60,80,60).contains(e->pos()))
    {
        emit btom();
    }
}

void StatisticsWidget::statistomain()
{
    emit btom();
}
